home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 553 < prev    next >
Encoding:
Text File  |  1996-08-06  |  7.3 KB  |  155 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Destruction of singletons
  5. Date: 26 Feb 1996 11:50:36 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <9602261828.AA28873@lts.sel.alcatel.de>
  9. References: <4fvng1$jit@netlab.cs.rpi.edu> <4go9au$l8f@engnews1.Eng.Sun.COM>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 26 Feb 96 19:28:57 +0100
  12. In-Reply-To: Jamshid Afshar's message of 25 Feb 1996 00:09:02 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTIPHUy4NqrwXLNJAQHKJAH9GAtsvu8gaLyjrUEw2C073PfxqKVy/t54
  15.     LrkuWJLDN+dN7kYUSo+GSVr0cirt95iSwjxg8au5um62NulSVn9YRA==
  16.     =bYxj
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article <4go9au$l8f@engnews1.Eng.Sun.COM> Jamshid Afshar
  20. <jamshid@IO.COM> writes:
  21.  
  22. |> In article <4fvng1$jit@netlab.cs.rpi.edu>,
  23. |> Bob Archer  <bob@hottub.demon.co.uk> wrote:
  24. |> >Singleton * Singleton::Instance() {
  25. |> >  if (_instance == 0) 
  26. |> >      _instance = new Singleton;
  27. |> >  return _instance;
  28. |> >}
  29. |> >
  30. |> >[Jamshid] suggested that using a static local variable might be better:
  31. |> >
  32. |> >Singleton * Singleton::Instance() {
  33. |> >  static Singleton s;
  34. |> >  return &s;
  35. |> >}
  36. |> >
  37. |> >James Kanze then replied:
  38. |> >    Although I tend to prefer this idiom too, it is important to realize
  39. |> >    that there are some cases where it is preferable *not* to destruct the
  40. |> >    variable.  (If there is only one, this shouldn't constitute a memory
  41. |> >    leak.)  [...]
  42. |> >
  43. |> >Why does the fact that only one variable is involved mean that this is 
  44. |> >not a memory leak?
  45.  
  46. |> James has responded that although technically it is a memory leak,
  47. |> it's not important unless you're using a poor operating system that
  48. |> does not recover all memory when a process finishes.  I agree I
  49. |> wouldn't want to use such an operating system (Win3.1?), but I think
  50. |> it is sloppy programming to not ensure all the objects you create get
  51. |> destroyed and all the memory you allocate gets deleted.  Of course
  52. |> there are probably cases where this is not possible or practical, but
  53. |> leaked memory and undestroyed objects cause hassles when using
  54. |> debugging tools like Purify which report all memory leaks when the
  55. |> program finishes.  You'll get enough leak reports from sloppy 3rd
  56. |> party libraries -- you shouldn't be adding a bunch "ignore this leak,
  57. |> I 'know' it's okay" configuration statements for the code you write.
  58. |> Also, you may sometime soon want to remove the "singleton" restriction
  59. |> on the class -- IMO get it right the first time and make sure its
  60. |> destructor works properly.
  61.  
  62. First of all, I will agree that not destructing the object is not the
  63. most elegant solution around, and in fact, now that I am aware of
  64. relatively simple programming techniques (returning the address of a
  65. local static) to ensure this, I use them.
  66.  
  67. Concerning the two practical objections, however:
  68.  
  69. 1. As long as there is an active pointer to the memory (a static
  70. pointer), Purify will not classify it as a leak.
  71.  
  72. 2. This technique is only used for *static* data member pointers.  If
  73. the class is no longer used as a singleton, then it is no problem
  74. putting the delete in the constructor.  The problem is where to put
  75. the delete otherwise.  How can you know that everyone has finished
  76. using the object.
  77.  
  78. In general, I would stress that the prefered solution is to not use
  79. the singleton object in any destructors, and use a local static to
  80. initialize the pointer.  If you do have to use the object in
  81. destructors, however, I would still prefer not destructing it to
  82. trying to manage the `order of destruction' problem (given that most
  83. compiler implementors still consider the order of destruction of
  84. static locals undefined).
  85.  
  86. |> >I take the point about the Singleton object possibly being used in the 
  87. |> >destructors of a static object. Is there any way to ensure that the 
  88. |> >singleton will be destroyed but only after everything that might wish 
  89. |> >to use it has been destroyed ? (I guess that this is just the usual 
  90. |> >problem with the order of global construction / destruction ).
  91.  
  92. |> Actually, the construction and destruction of "local objects of static
  93. |> storage duration" is not the same as objects outside of functions.  In
  94. |> fact, it seems local statics are more poorly specified.  According to
  95. |> the April '95 Draft, while they are guaranteed to not be constructed
  96. |> until (and if) the function is called, exactly when the destructor is
  97. |> called is unspecified.
  98.  
  99. I actually believe that it is, and in fact, has been well defined from
  100. the ARM.  The exact language in the sections concerning the
  101. initialization and destruction of statics is significant: in the
  102. sections concerning initializations, the reference is consistently to
  103. `non-local' statics, whereas in the section concerning destruction,
  104. the words `non-local' are significantly absent.  IMHO, the extreme
  105. care concerning the use of `non-local' in the initialization section
  106. makes it clear that its absense in the destruction section was not
  107. simply oversight, and that *all* statics were meant.
  108.  
  109. The perennial test suite contains a case which in fact does test this.
  110. Regretfully, the compiler implementors I'm familiar with have all
  111. denied that this was what was meant, and claimed that there was no
  112. guarantee concerning local statics.
  113.  
  114. In any case, the Jan. 96 draft makes the question moot, since it
  115. explicitly adds the words in the destructor section clarifying that
  116. static means both local and non-local.
  117.  
  118. |> At least globals are sensibly guaranteed to be
  119. |> destroyed in the reverse order they were constructed.  What should
  120. |> have been required IMO is that local statics be destroyed in the
  121. |> reverse order they were (fully) constructed.  For example, if the
  122. |> constructor of a local static "g_list" calls a function containing
  123. |> another local static "g_log", g_list should be destroyed first since
  124. |> its construction was finished last.  This is desirable behavior since
  125. |> g_list's destructor may also use g_log.
  126.  
  127. I agree.  The remaining weak point in the standard is that it doesn't
  128. really specify what is meant by the ordering, entering the
  129. constructor, or leaving it.
  130.  
  131.     [...]
  132. |> Is there any reason the committee chose not to have local statics do
  133. |> this kind of ordering automatically?  Of course, it won't solve every
  134. |> problem related to "global" construction/destruction ordering, but it
  135. |> seems a lot better than leaving it unspecified and doesn't seem like
  136. |> it would have placed any burden whatsoever on implementors.
  137.  
  138. It does place a burden on the implementors, since in fact, the order
  139. of destruction will depend upon the order the functions are called.
  140. But the committe, and Bjarne before them, voted in favor of the
  141. programmer, not the implementor.
  142.  
  143. --
  144. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  145. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  146. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  147.                 -- A la recherche d'une activitΘ dans une region francophone
  148. ---
  149. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  150.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  151.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  152.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  153.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  154. ]
  155.